package top.healthylife.gateway.mgr.controller;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import top.healthylife.basics.utils.utils.R;
import top.healthylife.basics.utils.utils.RedisUtil;
import top.healthylife.gateway.dto.request.base.SystemInfoDto;
import top.healthylife.gateway.mgr.component.ApiComponent;
import top.healthylife.gateway.mgr.component.GatewaySaveLogComponent;
import top.healthylife.gateway.mgr.service.GatewayAccountService;
import top.healthylife.gateway.mgr.service.GatewayLogService;
import top.heathylife.gateway.common.cons.SystemCons;
import top.heathylife.gateway.common.po.SystemPo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author maoxiaomeng
 */
@Api(tags = "数据网关服务-平台接口")
@Slf4j
@RestController
@RequestMapping("/gateway/platform")
@AllArgsConstructor
public class PlatformController {
    ApiComponent apiComponent;
    RestTemplate restTemplate;
    GatewayAccountService gatewayAccountService;
    GatewaySaveLogComponent gatewaySaveLogComponent;
    GatewayLogService gatewayLogService;


    @ApiOperation("服务商列表")
    @GetMapping("/list")
    public R<List<SystemPo>> platform(@RequestParam(required = false) String systemName) {
        List<SystemPo> list = RedisUtil.list(SystemCons.platformKey(), SystemPo.class);
        if (ObjectUtil.isNotEmpty(systemName)) {
            list = list.stream().filter(e -> e.getSystemName().contains(systemName)).collect(Collectors.toList());
        }
        return R.ok(list);
    }


    @ApiOperation("服务商账户配置信息")
    @GetMapping("/account/config/template/{shortName}")
    @SuppressWarnings("unchecked")
    public R<List<SystemInfoDto>> accountConfigTemplate(@PathVariable String shortName) {
        SystemPo systemPo = apiComponent.checkSystemPo(shortName);
        return restTemplate.getForObject(SystemCons.getSystemConfigUrl(systemPo.getApplicationName()), R.class);
    }


    @ApiOperation("服务商详情")
    @GetMapping("/{shortName}")
    public R<SystemPo> getSystemInfo(@PathVariable String shortName) {
        return R.ok(apiComponent.checkSystemPo(shortName));
    }

}
